// openmetadata-ui/src/main/resources/ui/src/utils/LocalStorageUtils.ts

export const OM_SESSION_KEY = "om-session";
const INITIAL_OM_SESSION = "{\"state\":{}}";

export const getOidcToken = (): string => {
  return (
    JSON.parse(localStorage.getItem(OM_SESSION_KEY) ?? INITIAL_OM_SESSION)?.state?.oidcIdToken ?? ""
  );
};

export const setOidcToken = (token: string) => {
  const session = JSON.parse(localStorage.getItem(OM_SESSION_KEY) ?? INITIAL_OM_SESSION);

  session.state.oidcIdToken = token;
  localStorage.setItem(OM_SESSION_KEY, JSON.stringify(session));
};

export const getRefreshToken = (): string => {
  return (
    JSON.parse(localStorage.getItem(OM_SESSION_KEY) ?? INITIAL_OM_SESSION)?.state?.refreshTokenKey ??
    ""
  );
};

export const setRefreshToken = (token: string) => {
  const session = JSON.parse(localStorage.getItem(OM_SESSION_KEY) ?? INITIAL_OM_SESSION);

  session.state.refreshTokenKey = token;
  localStorage.setItem(OM_SESSION_KEY, JSON.stringify(session));
};
